package com.macoli.android_test;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Debug;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class MemoryUtils {
/*
    public static long getProcessRealMemory() {
        String memFilePath = "/proc/" + android.os.Process.myPid() + "/status";
        BufferedReader bufferedReader = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(memFilePath);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                Log.d(Constants.TAG, " read line : " + line);
                if(!TextUtils.isEmpty(line) && line.contains("VmRSS")) {
                    String rss = line.split(":")[1].trim().split( " ")[0];
                    return Integer.parseInt(rss) * 1024;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader != null) {
                try{
                    bufferedReader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return -1;
    }

    public static int getProcessRealMemoryByAm(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        final Debug.MemoryInfo[] memInfo = activityManager.getProcessMemoryInfo(new int[]{android.os.Process.myPid()});
        return memInfo[0].getTotalPss();
    }

    public static int getProcessRealMemoryByDebug() {
        Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
        Debug.getMemoryInfo(memoryInfo);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String javaHeap = memoryInfo.getMemoryStat("summary.java-heap");
            String nativeHeap = memoryInfo.getMemoryStat("summary.native-heap");
            String code = memoryInfo.getMemoryStat("summary.code");
            String stack = memoryInfo.getMemoryStat("summary.stack");
            String graphics = memoryInfo.getMemoryStat("summary.graphics");
            String privateOther = memoryInfo.getMemoryStat("summary.private-other");
            String system = memoryInfo.getMemoryStat("summary.system");
            String swap = memoryInfo.getMemoryStat("summary.total-swap");
        }
        return memoryInfo.getTotalPss() ;
    }*/
}
